package com.fml.ft_study.view;
import com.fml.ft_study.domain.Bill;
import com.fml.ft_study.domain.DiningTable;
import com.fml.ft_study.domain.Employee;
import com.fml.ft_study.domain.menu;
import com.fml.ft_study.service.BillService;
import com.fml.ft_study.service.DiningTableService;
import com.fml.ft_study.service.EmployeeService;
import com.fml.ft_study.service.MenuService;
import com.fml.ft_study.utils.Utility;
import java.util.List;
public class MHLview {
    private boolean loop = true;
    private String key = "";
    private EmployeeService employeeService = new EmployeeService();
    private DiningTableService diningTableService=new DiningTableService();
    private MenuService menuService=new MenuService();
    private BillService billService=new BillService();
    public static void main(String[] args) {
        new MHLview().mhlmuses();

    }
public void listDiningTable(){
    List<DiningTable> list = diningTableService.list();
    System.out.println("座位号"+"\t\t"+"座位状态");
    for (DiningTable diningTable : list) {
        System.out.println(diningTable.getId()+"\t\t"+diningTable.getState());
    }
}
//订餐系统的完成
public void orderDiningTable(){
    System.out.println("===============餐桌预定===============");
    System.out.println("输入你要预定的餐桌号（-1退出）");
    String les = Utility.readString(50);
    if (les.equals("-1"))return;
    DiningTable diningTableById = diningTableService.getDiningTableById(les);
    if (diningTableById!=null){
        if (("空".equals(diningTableById.getState()))){
//            System.out.println("确定预定吗Y/N");
            char c = Utility.readConfirmSelection();
          if (c=='Y'){
              System.out.println("请输入预定人姓名：");
              String orderName = Utility.readString(50);
              System.out.println("请输入预订人电话：");
              String ordertel = Utility.readString(50);
              boolean b = diningTableService.updataById(orderName,ordertel);
              if (b){ System.out.println("预定成功");
                  return;
              }
              else{ System.out.println("预定失败");
              return;
              }

          }else {
              System.out.println("已经取消选择");
              return;
          }
        }else {
            System.out.println("此座位已经被预定");
            return;
        }

    }else {
        System.out.println("输入的座位错误");
        return;
    }
}
//菜单显示
    public void listMenu(){
        List<menu> menus = menuService.listMenu();
        System.out.println("菜名:\t\t"+"类型:\t\t"+"价格:\t\t");
        for (menu menu : menus) {
            System.out.println(menu.getName()+"\t\t"+menu.getType()+"\t\t"+menu.getPrice());
        }

    }
    //点餐服务
    public  void orderMenu(){
        System.out.println("\n===============点餐服务================");
        int diningTableId=0;
        int meunId=0;
        int nums=0;
        while (meunId!=-1&&diningTableId!=-1&&nums!=-1) {
            System.out.println("请输入点餐的桌号（-1退出）");
            diningTableId = Utility.readInt();
            if (diningTableId==-1)break;
            DiningTable diningTable = diningTableService.diningTableById(diningTableId);
            if (diningTable==null){
                System.out.println("没有这个餐桌");
                continue;
            }else {
                break;
            }
        }

        while (meunId!=-1&&diningTableId!=-1&&nums!=-1) {
            System.out.println("请输入菜品号（-1退出）");
            meunId = Utility.readInt();
            if (meunId==-1)break;
            menu menu = menuService.MenuById(meunId);
            if (menu==null){
                System.out.println("没有这个菜品");
                continue;
            }else {
                break;
            }
        }
        while (meunId!=-1&&diningTableId!=-1&&nums!=-1) {
            System.out.println("请输入菜数量（-1退出）");
            nums = Utility.readInt();
            if (nums==-1)break;
            if (nums<=0){
                System.out.println("数量输入错误");
                continue;
            }else {
                break;
            }
        }
        if (meunId==-1||diningTableId==-1||nums==-1){
            System.out.println("退出点餐系统");
            return;
        }
        boolean b = billService.updataBill(meunId,nums,diningTableId);
        if (b) System.out.println("点菜成功");
        else System.out.println("点菜错误");
    }
//    listBill();//显示所有账单
    public void listBill(){
        List<Bill> bills = billService.listBills();
        System.out.println("编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期");
        for (Bill bill : bills) {
            System.out.println(bill.toString());
        }
    }
//    payBill();结账方法
    public void payBill(){
        System.out.println("\n===============结账服务================");
        System.out.println("输入要结账的桌号(-1取消)");
        int diningTable = Utility.readInt();
        if (diningTable==-1)return;
       if(billService.NostateById(diningTable)){//判断餐桌有没有没有支付的账单
           System.out.println("结账的方式支付宝、微信、现金");
           String modePay = Utility.readString(50);
         if (billService.PayBill(diningTable,modePay)) {//判断更新状态是否成功
             System.out.println("结账成功");
         }else {
             System.out.println("结账失败");

         }

       } else {
           System.out.println("没有这个餐桌或者此餐桌没有待支付的账单");
       }
    }
    public void mhlmuses() {
        while (loop) {
            System.out.println("\n===============满汉楼================");
            System.out.println("\t\t 1 登录满汉楼");
            System.out.println("\t\t 2 退出满汉楼");
            System.out.print("请输入你的选择: ");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("输入员工号: ");
                    String empId = Utility.readString(50);
                    System.out.print("输入密  码: ");
                    String pwd = Utility.readString(50);
                    Employee employee = employeeService.login(empId,pwd);
                    if (employee != null) { //说明存在该用户
                        System.out.println("===============登录成功[" + employee.getName() + "]================\n");
                        //显示二级菜单, 这里二级菜单是循环操作，所以做成while
                        while (loop) {
                            System.out.println("\n===============满汉楼(二级菜单)================");
                            System.out.println("\t\t 1 显示餐桌状态");
                            System.out.println("\t\t 2 预定餐桌");
                            System.out.println("\t\t 3 显示所有菜品");
                            System.out.println("\t\t 4 点餐服务");
                            System.out.println("\t\t 5 查看账单");
                            System.out.println("\t\t 6 结账");
                            System.out.println("\t\t 9 退出满汉楼");
                            System.out.print("请输入你的选择: ");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    listDiningTable();//显示餐桌状态
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    listMenu();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();//显示所有账单
                                    break;
                                case "6":
                                    payBill();
                                    break;
                                case "9":
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("你的输入有误，请重新输入");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("===============登录失败================");
                    }
                    break;
                case "2":
                    loop = false;//
                    break;
                default:
                    System.out.println("你输入有误，请重新输入.");
            }
        }
        System.out.println("你退出了满汉楼系统~");
    }
}
